package com.pandora.common.util;

import com.pandora.framework.util.LogUtils;

import sun.net.util.IPAddressUtil;

/**
 * 校验工具
 * 
 * @author JingChenglong 2017/12/20 14:26
 *
 */
public class VerifyUtils {

	/** 数字 */
	public static final String MATCH_NUMBER = "[0-9]+";
	/** Email */
	public static final String MATCH_EMAIL = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
	/** 11位大陆手机号码 */
	public static final String MACTH_MOBILE11 = "^1[3,4,5,7,8][0-9]{9}$";
	/** 中国大陆身份证号 */
	public static final String MATCH_IDCARDNO = "^\\d{15}$|^\\d{17}[0-9Xx]$";
	/** 中国大陆邮政编码 */
	public static final String MATCH_POSTCODE = "[1-9][0-9]+";
	/** MD5签名结果 */
	public static final String MATCH_MD5 = "[0-9A-Fa-f]+";

	/**
	 * 正则校验字符串格式
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static boolean checkString(String str, String regex) {

		return str.matches(regex);
	}

	/**
	 * 校验字符串是否为数值
	 * 
	 * @param str
	 * @return
	 */
	public static boolean checkNumber(String number) {

		number = StringUtils.nullToStrTrim(number);

		if (StringUtils.isBlank(number) || number.split(".").length > 2 || !number.matches(MATCH_NUMBER)) {
			return false;
		}

		try {
			Double.valueOf(number);
		} catch (Exception e) {
			LogUtils.logError(CommonUtils.getTraceInfo() + e.getMessage());
			return false;
		}

		return true;
	}

	/**
	 * 校验字符串是否为Email格式
	 * 
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String email) {

		email = StringUtils.nullToStrTrim(email);

		if (StringUtils.isEmpty(email) || !StringUtils.isHalfAngle(email) || email.length() < 5
				|| email.length() > 60) {
			return false;
		}

		return email.matches(MATCH_EMAIL);
	}

	/**
	 * 校验是否为11位大陆手机号码
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean checkMobile11(String mobile) {

		mobile = StringUtils.nullToStrTrim(mobile);

		if (StringUtils.isEmpty(mobile) || !checkNumber(mobile)) {
			return false;
		}

		return mobile.matches(MACTH_MOBILE11);
	}

	/**
	 * 校验中国大陆居民省份证号
	 * 
	 * @param idCardNo
	 * @return
	 */
	public static boolean checkIdCardNo(String idCardNo) {

		idCardNo = StringUtils.nullToStrTrim(idCardNo);

		if (!StringUtils.isHalfAngle(idCardNo) || (idCardNo.length() != 15 && idCardNo.length() != 18)) {
			return false;
		}

		return idCardNo.matches(MATCH_IDCARDNO);
	}

	/**
	 * 校验是否为中国大陆邮政编码
	 * 
	 * @param postCode
	 * @return
	 */
	public static boolean checkPostCode(String postCode) {

		postCode = StringUtils.nullToStrTrim(postCode);

		if (!checkNumber(postCode) || postCode.length() != 6) {
			return false;
		}

		return postCode.matches(MATCH_POSTCODE);
	}

	/**
	 * 校验IP地址
	 * 
	 * @param ip
	 * @return
	 */
	public static boolean checkIp(String ip) {

		ip = StringUtils.nullToStrTrim(ip);

		if (StringUtils.isEmpty(ip) || StringUtils.isBlank(ip) || !StringUtils.isHalfAngle(ip) || ip.length() < 7) {
			return false;
		}

		return IPAddressUtil.isIPv4LiteralAddress(ip) || IPAddressUtil.isIPv6LiteralAddress(ip);
	}

	/**
	 * 校验字符串格式是否为MD5格式值
	 * 
	 * @param str
	 * @return
	 */
	public static boolean checkMD5(String str) {

		if (StringUtils.isEmpty(str) || str.length() != 32) {
			return false;
		}
		return checkString(str, MATCH_MD5);
	}

	/**
	 * 校验数值时间格式
	 * 
	 * @param reqTime
	 * @return
	 */
	public static boolean checkTime(String reqTime) {

		if (StringUtils.isEmpty(reqTime) || reqTime.length() != 14) {
			return false;
		}

		if (!checkString(reqTime, "[2][0-9]+")) {
			return false;
		}

		return TimeUtils.validTime(reqTime, TimeUtils.yyyyMMddHHmmssNoCut.toPattern());
	}

	public static void main(String[] args) {
		System.out.println(TimeUtils.yyyyMMddHHmmssNoCut.toPattern());
	}
}